<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:business:place:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_place_form' })">
              {{ i18n('common.create') }}
            </el-button>

            <el-button
              v-if="isAuth('module:business:place:category:list')"
              class="el-button--cyan"
              icon="Filter"
              @click="router.push({ name: 'module_business_place_category_list' })">
              {{ i18n('place.category.info') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:place:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.category_id"
            :placeholder="i18n('common.please_select') + i18n('place.category_id')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-cascader v-model="dataForm.region_id" :placeholder="$t('common.please_select')+$t('place.address')" :options="addressList" :props="levelProps" clearable></el-cascader>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.cooperation_level"
            :placeholder="i18n('common.please_select') + i18n('place.cooperation_level')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in cooperationLevelList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.star_level"
            :placeholder="i18n('common.please_select') + i18n('place.star_level')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in starLevelList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('place.title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.is_recommend" :placeholder="$t('common.please_select') + $t('place.is_recommend')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option :label="$t('common.recommended')" value="1"></el-option>
            <el-option :label="$t('common.unrecommended')" value="2"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.is_hidden" :placeholder="$t('common.please_select') + $t('common.is_hidden')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option :label="$t('common.hidden')" value="1"></el-option>
            <el-option :label="$t('common.show')" value="2"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">{{ scope.row.organization.title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('place.info')" min-width="300">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  <b>{{ scope.row.title }}</b>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.category.title }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('place.cooperation_level')" width="100">
            <template v-slot="scope">
              {{ scope.row.cooperation_level.value }} - {{ scope.row.cooperation_level.text }}
            </template>
          </el-table-column>

          <el-table-column :label="$t('place.star_level')" width="140">
            <template v-slot="scope">
              <el-rate v-model="scope.row.star_level.value" disabled></el-rate>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('place.address')" min-width="300">
            <template v-slot="scope">
              <dl class="table_dl">
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.province_id.text }} - {{ scope.row.city_id.text }} - {{ scope.row.region_id.text }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.address }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column :label="$t('place.is_recommend')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_recommend.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'is_recommend')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('common.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:place:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_place_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:place:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="dialogVisible = true;loadQrcodeUrl(scope.row.id, scope.row.organization_id)">
                      {{ i18n('place.preview') }}
                    </el-dropdown-item>
                    <el-dropdown-item v-if="isAuth('module:business:place:contact:list')" @click="router.push({ name: 'module_business_place_contact_list', query: { place_id: scope.row.id } })">
                      {{ i18n('place.contact.info') }}
                    </el-dropdown-item>
                    <el-dropdown-item v-if="isAuth('module:business:place:photo:category:list')" @click="router.push({ name: 'module_business_place_photo_category_list', query: { place_id: scope.row.id } })">
                      {{ i18n('place.photo.category.info') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="在小程序上查看" width="300" center>
      <div class="text-center mt20">
        <el-image class="qrcode" :src="qrcode_url"/>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="place-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/place')

  const levelProps = ref({
    label: 'title',
    value: 'id',
    emitPath: false,
    checkStrictly: true
  })

  const dialogVisible = ref(false)
  const qrcode_url = ref('')
  const organizationList = ref([])
  const categoryList = ref([])
  const addressList = ref([])
  const cooperationLevelList = ref([])
  const starLevelList = ref([])

  const dataForm = reactive([
    'organization_id',
    'category_id',
    'region_id',
    'title',
    'cooperation_level',
    'star_level',
    'is_recommend',
    'is_hidden',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/business/category/select'),
      method: 'get',
      params: $http.adornParams({
        type: 3,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAddressList = () => {
    $http({
      url: $http.adornUrl('/common/area/three'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        addressList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadCooperationLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'cooperation_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        cooperationLevelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadStarLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'star_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        starLevelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadQrcodeUrl = (id, organization_id) => {
    $http({
      url: $http.adornUrl('/business/qrcode/mini/data'),
      method: 'get',
      params: $http.adornParams({
        organization_id: organization_id,
        path: '/pages/resource/base/detail?id='+id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        qrcode_url.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadOrganizationList()
    loadCategoryList()
    loadAddressList()
    loadCooperationLevelList()
    loadStarLevelList()
  })
</script>

<style scoped lang="scss">
  .qrcode {
    width: 10vw;
    height: 10vw;
  }
</style>
